package com.gcxy.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gcxy.dto.Dic.DicPageDto;
import com.gcxy.dto.Dic.InsertDto;
import com.gcxy.entity.Dic;
import com.gcxy.entity.Ranch;
import com.gcxy.mapper.DicMapper;
import com.gcxy.mapper.RanchMapper;
import com.gcxy.service.DicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gcxy.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wlp
 * @since 2023-12-19
 */
@Service
public class DicServiceImpl extends ServiceImpl<DicMapper, Dic> implements DicService {
    @Autowired
    private DicMapper dicMapper;
    @Autowired
    private RanchMapper ranchMapper;
    @Override
    public R addDic(InsertDto requestBody) {
        LambdaQueryWrapper<Dic> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dic::getDicVal,requestBody.getDicVal());
        Dic dic = dicMapper.selectOne(lambdaQueryWrapper);
        if (dic!=null)
        {
            return R.Failed(0,"新增失败，字典已存在");
        }
        Dic dic1=Dic.builder()
                .dicVal(requestBody.getDicVal())
                .dicState(1)
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build();
        int insert = dicMapper.insert(dic1);
        if (insert>0){
            return R.Success();
        }
        return R.Failed(0,"新增失败");
    }

    @Override
    public R getAll() {
        LambdaQueryWrapper<Dic> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dic::getDicState,1);
        List<Dic> dics = dicMapper.selectList(lambdaQueryWrapper);
        return R.Success(dics);
    }

    @Override
    public R pageDic(DicPageDto requestBody) {
        QueryWrapper<Dic> queryWrapper=new QueryWrapper<>();
        Page<Dic> page=new Page<>(requestBody.getPageNumber(),requestBody.getPageSize());
            queryWrapper.like(requestBody.getDicVal()!=null&&!"".equals(requestBody.getDicVal()),"dic_val",requestBody.getDicVal())
                    .eq(requestBody.getDicState()!=null,"dic_state",requestBody.getDicState())
                    .orderByDesc("create_time");
        dicMapper.selectPage(page,queryWrapper);
        return R.Success(page);
    }

    @Override
    public R updateState(Integer dicId, Integer dicState) {
        LambdaQueryWrapper<Dic> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dic::getDicId,dicId);
        Dic dic=Dic.builder()
                .dicState(dicState)
                .build();
        LambdaQueryWrapper<Ranch> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Ranch::getDicId,dicId);
        Ranch ranch = ranchMapper.selectOne(queryWrapper);
        if(ranch!=null){
            return R.Failed(0,"修改失败");
        }
        int update = dicMapper.update(dic, lambdaQueryWrapper);
        if(update>0){
            return R.Success();
        }
        return R.Failed(0,"修改失败");
    }
}
